package com.meituan.catering.management.shop.biz.service.impl;

import com.meituan.catering.management.shop.api.http.model.request.CloseShopHttpRequest;
import com.meituan.catering.management.shop.api.http.model.request.CreateShopHttpRequest;
import com.meituan.catering.management.shop.api.http.model.request.OpenShopHttpRequest;
import com.meituan.catering.management.shop.api.http.model.request.UpdateShopHttpRequest;
import com.meituan.catering.management.shop.biz.model.ShopBO;
import com.meituan.catering.management.shop.biz.model.converter.ShopBOConverter;
import com.meituan.catering.management.shop.biz.service.ShopBizOperateService;
import com.meituan.catering.management.shop.dao.mapper.ShopMapper;
import com.meituan.catering.management.shop.dao.model.ShopDO;
import com.meituan.catering.management.shop.util.BusinessNoUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

/**
 * @Author:ale
 * @Date 2022/4/23 16:31
 * @Version 1.0
 */
@Service
public class ShopBizOperateServiceImpl implements ShopBizOperateService {

    @Resource
    TransactionTemplate transactionTemplate;

    @Resource
    private ShopMapper shopMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ShopBO create(Long tenantId, CreateShopHttpRequest request) {
        return transactionTemplate.execute(status -> {
            String businessNo = BusinessNoUtils.generateBusinessNo().toString();
            int insertRowCount = shopMapper.create(tenantId, businessNo, request);
            if (insertRowCount != 1) {
                status.setRollbackOnly();
                throw new IllegalStateException("创建新门店失败");
            }
            ShopDO shopDO = shopMapper.findByBusinessNo(businessNo);
            return ShopBOConverter.fromDO(shopDO);
        });
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ShopBO update(String businessNo, UpdateShopHttpRequest request) {

        return transactionTemplate.execute(status ->{
            int updateRowCount = shopMapper.update(businessNo,request);
            if (updateRowCount != 1){
                status.setRollbackOnly();
                throw new IllegalStateException("更新门店失败！");
            }
            ShopDO shopDO = shopMapper.findByBusinessNo(businessNo);
            return ShopBOConverter.fromDO(shopDO);
        });
    }

    @Override
    public ShopBO close(String businessNo, CloseShopHttpRequest request) {
        return transactionTemplate.execute(status ->{
            int closeRowCount = shopMapper.close(businessNo,request);
            if (closeRowCount != 1){
                status.setRollbackOnly();
                throw new IllegalStateException("关闭门店失败！");
            }
            ShopDO shopDO = shopMapper.findByBusinessNo(businessNo);
            return ShopBOConverter.fromDO(shopDO);
        });
    }

    @Override
    public ShopBO open(String businessNo, OpenShopHttpRequest request) {
        return transactionTemplate.execute(status ->{
            int openRowCount = shopMapper.open(businessNo,request);
            if (openRowCount != 1){
                status.setRollbackOnly();
                throw new IllegalStateException("关闭门店失败！");
            }
            ShopDO shopDO = shopMapper.findByBusinessNo(businessNo);
            return ShopBOConverter.fromDO(shopDO);
        });
    }
}